<template>
  <div class="main">
    <el-dialog
      :title="title"
      :visible.sync="dialogVisible"
      class="detail-court"
      width="40%"
      :append-to-body="true"
      :before-close="handleClose"
      destroy-on-close
    >
      <el-form
        :model="formData"
        :rules="rules"
        ref="ruleFormRef"
        label-width="140px"
        label-suffix="："
      >
        <el-form-item label="充值金额（元）" prop="rechargeAmount">
          <el-col :span="12">
            <el-input
              placeholder="请输入充值金额"
              v-model.number="formData.rechargeAmount"
              type="number"
              :step="0.01"
              autocomplete="off"
            ></el-input>
          </el-col>
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="onSubmit" :loading="loading"
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>
<script>
// import { amountApply } from "@/api/bill.js";
export default {
  name: "rechargeDialog",
  props: {
    data: {
      type: Array,
      default: () => [],
    },
    title: {
      type: String,
      default: "充值",
    },
  },
  data() {
    return {
      rules: {
        rechargeAmount: [
          { required: true, message: "减免后金额不能为空" },
          { type: "number", message: "减免后金额必须为数字值" },
        ],
      },
      loading: false,
      dialogVisible: false,
      formData: {
        id: "",
        rechargeAmount: "",
      },
    };
  },
  mounted() {},
  methods: {
    async submitData() {
      try {
        // console.log("on submit", this.formData);
        this.loading = true;
        const res = await this.$http.post(
          "/loanMarket/rechargeAmount",
          this.formData
        );
        // console.log("recharge res: ", res);
        if (res.data?.code == 200) {
          this.$message({ type: "success", message: res.data?.msg });
          this.$refs.ruleFormRef?.resetFields();
        } else {
          this.$message.error(res.data?.msg);
          return;
        }
        this.dialogVisible = false;
      } catch (error) {
        console.log(error);
      } finally {
        this.loading = false;
      }
    },
    showConfirm() {
      this.$confirm(`确定${this.title}吗？`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          await this.submitData();
        })
        .catch(() => {});
    },
    onSubmit() {
      this.$refs.ruleFormRef.validate((valid) => {
        if (valid) {
          this.showConfirm();
        } else {
          return false;
        }
      });
    },

    open(id) {
      console.log("id", id);
      this.formData.id = id;
      this.dialogVisible = true;
    },
    handleClose(done) {
      this.$refs.ruleFormRef?.resetFields();
      done();
    },
  },
};
</script>
<style scoped lang="scss"></style>
